<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <el-card>
    <el-row>
      <el-col>
        <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      </el-col>
    </el-row>

    <el-table :data="cateList" style="width: 100%" row-key="cat_id" :tree-props="{children: 'children'}">
      <el-table-column label="#" type="index" prop="id" width="180"/>
      <el-table-column prop="cat_name" label="分类名称" width="180"/>
      <el-table-column label="是否有效">

        <template v-slot:default="scope">
          <el-icon v-if="!scope.row.cat_deleted"><Select/></el-icon>
          <el-icon v-if="scope.row.cat_deleted">
            <CloseBold/>
          </el-icon>
        </template>
      </el-table-column>

      <el-table-column label="排序">
        <template v-slot:default="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level === 2" type="warning">三级</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-button type="primary" size="small">
            <el-icon>
              <EditPen/>
            </el-icon>&nbsp;&nbsp;编辑
          </el-button>
          <el-button type="danger" size="small">
            <el-icon>
              <Delete/>
            </el-icon>&nbsp;&nbsp;删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        v-model:current-page="queryInfo.pagenum"
        v-model:page-size="queryInfo.pagesize"
        :page-sizes="[3, 5, 10, 15]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
    />

  </el-card>


  <el-dialog
      v-model="addCateDialogVisible"
      title="添加分类"
      width="50%"
      @close="addCateDialogClosed"
  >
    <el-form :model="addCateForm"
             :rules="addCateFormRules"
             ref="addCateFormRef"
             label-width="100px"
    >
      <el-form-item label="分类名称&nbsp;:" prop="cat_name">
        <el-input v-model="addCateForm.cat_name"></el-input>
      </el-form-item>

      <el-form-item label="父级分类&nbsp;:">
        <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateHandleChange"
            clearable
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addCate">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

</template>

<script>
export default {
  name: "Cate.vue",
  data() {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 10, message: '分类名称的长度在 2 到 10 之间', trigger: 'blur'},
        ],
      },
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      selectedKeys: []
    }
  }, created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const {data: res} = await this.$http.get('/categories', {params: this.queryInfo})
      if (res.meta.status === 200) {
        this.cateList = res.data.result
        this.total = res.data.total
        // console.log(res.data.result)
      }
      return (res.meta.status === 200) ? this.$message.success(res.meta.msg) : this.$message.error(res.meta.msg);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getCateList()
    },
    showAddCateDialog() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    async getParentCateList() {
      const {data: res} = await this.$http.get('/categories', {
        params: {
          type: 2,
        }
      })
      if (res.meta.status !== 200) {
        return this.$message.error("获取父级分类数据失败")
      }
      this.parentCateList = res.data
    },
    parentCateHandleChange() {
      //console.log(this.selectedKeys)
      if (this.selectedKeys !== null) {
        if (this.selectedKeys.length > 0) {
          this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
          this.addCateForm.cat_level = this.selectedKeys.length
          return
        } else {
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
      }
    },
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {

        if (valid) {
          const {data: res} = await this.$http.post('/categories', this.addCateForm)

          if (res.meta.status === 201) {
            await this.getCateList()
            this.addCateDialogVisible = false
          }
          return (res.meta.status === 201) ? this.$message.success(res.meta.msg) : this.$message.error(res.meta.msg);
        }
      })
    },
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields();
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>

<style scoped>

</style>